package com.liyuxiang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liyuxiang.pojo.entity.Article;
import com.liyuxiang.pojo.entity.Comment;
import com.liyuxiang.pojo.entity.LoginUser;
import com.liyuxiang.pojo.vo.CommentVo;
import com.liyuxiang.pojo.vo.PageVo;
import com.liyuxiang.service.CommentService;
import com.liyuxiang.mapper.CommentMapper;
import com.liyuxiang.utils.BeanCopyUtils;
import com.liyuxiang.utils.SecurityUtils;
import com.liyuxiang.vm.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author liyuxiang
 * @description 针对表【sg_comment(评论表)】的数据库操作Service实现
 * @createDate 2022-03-29 16:22:07
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment>
        implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Override
    public ResponseResult getCommentList(Long articleId, Integer pageNum, Integer pageSize) {
//        Page page = new Page(pageNum, pageSize);
//        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<Comment>();
//        //获取文章对应的评论
//        queryWrapper.eq(Comment::getArticleId, articleId);
//        //获取根评论id
//        queryWrapper.eq(Comment::getRootId,-1);
//        page(page, queryWrapper);
//        List<CommentVo> commentVoList = BeanCopyUtils.copyBeanList(page.getRecords(), CommentVo.class);
        Page<CommentVo> page = new Page<>(pageNum, pageSize);
        List<CommentVo> commentVoList = commentMapper.getCommentListPage(page, articleId);
        for (CommentVo commentVo : commentVoList) {
            List<CommentVo> commentchildrenList = getCommentchildrenList(commentVo.getId());
            commentchildrenList.forEach(commentVo1 -> System.out.println(commentVo1.toString()));
            commentVo.setChildren(commentchildrenList);
        }
//        commentVoList.forEach(commentVo -> System.out.println(commentVo.toString()));
        return ResponseResult.okResult(new PageVo(commentVoList, (int) page.getTotal()));
    }

    @Override
    public ResponseResult addComment(Comment comment) {
        //获取登录人用户id
        Long loginUserId = SecurityUtils.getUserId();
        comment.setCreateBy(loginUserId);
        comment.setCreateTime(new Date());
        comment.setUpdateBy(loginUserId);
        comment.setUpdateTime(new Date());
        System.out.println(comment.toString());
        Integer result = commentMapper.addComment(comment);
        System.out.println(result);
        return ResponseResult.okResult(null);
    }


    public List<CommentVo> getCommentchildrenList(Long commentId) {
        List<CommentVo> commentVoList = commentMapper.getCommentchildrenList(commentId);
        return commentVoList;
    }
}




